package souralven.jcomicreader.controller.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;

import souralven.jcomicreader.controller.ComicController;
import souralven.jcomicreader.ui.FormatFilter;

public class OpenFileListener implements ActionListener {
	private ComicController controller;

	public OpenFileListener(final ComicController controller) {
		super();
		this.controller = controller;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JFileChooser chooser = new JFileChooser();
		chooser.setMultiSelectionEnabled(false);
		chooser.setFileFilter(new FormatFilter());
		// TODO make it modal
		int returnVal = chooser.showOpenDialog(null);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			try {
				File file = chooser.getSelectedFile();
				controller.showComic(file);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}
}